package com.crealth.handler;

import java.util.List;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.crealth.ConstantDefine;
import com.crealth.db.CityDistrictDb;
import com.crealth.memcached.CityDistrictCached;
import com.crealth.memcached.LendingAgencyCached;
import com.crealth.memcached.UserCached;
import com.crealth.model.CityDistrictModel;
import com.crealth.model.CompanyModel;
import com.crealth.model.UserModel;
import com.crealth.msg.NetMessageData;

public class CityDistrictHandler {
	//private static Logger logger = Logger.getLogger("running_log");
	
	public static NetMessageData obtainCityDistrict(NetMessageData revData) {
		JSONObject msgJson = revData.getRevDataMsgJson();
		NetMessageData sendMsg = new NetMessageData();
		JSONObject resJson = new JSONObject();
		int userID = msgJson.getIntValue("id");
		UserModel user = UserCached.selectUserByID(userID);
		List<CityDistrictModel> allDistrictyls = CityDistrictCached.selectAllCityDistrict();
		if (allDistrictyls != null && allDistrictyls.size() > 0) {			
			JSONArray resJsonArr = new JSONArray();
			CompanyModel belongCom = user.getBelongCompany();
			for (CityDistrictModel district : allDistrictyls) {
				if (user.getPosition() >= ConstantDefine.Head_Manager || 
					belongCom == null || belongCom.getCityName().length() == 0 || 
					belongCom != null && belongCom.getCityName().length() > 0 && 
					district.getCityName().length() > 0 &&  
					belongCom.getCityName().equals(district.getCityName())  ) {
					resJsonArr.add(district.toJSONObject());
				}
			}
			resJson.put("allDistrictJson", resJsonArr);
		}
		resJson.put("resCode", 0);
		sendMsg.setMsgBody(resJson.toJSONString());
		return sendMsg;
	}

	public static NetMessageData saveCityDistrict(NetMessageData revData) {
		NetMessageData sendMsg = new NetMessageData();
		JSONObject resJson = new JSONObject();
		JSONObject msgJson = revData.getRevDataMsgJson();
		String allDistrictJson = msgJson.getString("allDistrictJson");
		JSONArray jsonArray = JSONArray.parseArray(allDistrictJson);
		if (jsonArray != null && jsonArray.size() > 0) {
			JSONArray resJsonArr = new JSONArray();
			for (int i = 0; i < jsonArray.size(); ++i) {
				JSONObject agencyJson = jsonArray.getJSONObject(i);
				CityDistrictModel district = new CityDistrictModel();
				district.parseFromJson(agencyJson);
				if (district.getId() > 0) {
					CityDistrictDb.updateCityDistrict(district);
				} else {
					CityDistrictDb.insertCityDistrict(district);
				}
				resJsonArr.add(district.toJSONObject());
			}
			
			resJson.put("allDistrictJson", resJsonArr);
			CityDistrictCached.deleteCached();
		}		
		
		resJson.put("resCode", 0);
		sendMsg.setMsgBody(resJson.toJSONString());
		return sendMsg;
	}
	
	public static NetMessageData delCityDistrict(NetMessageData revData) {
		NetMessageData sendMsg = new NetMessageData();
		JSONObject resJson = new JSONObject();
		JSONObject msgJson = revData.getRevDataMsgJson();
		int districtID = msgJson.getIntValue("districtID");
		Integer res = CityDistrictDb.deleteCityDistrictByID(districtID);
		if (res != null && res == 1) {
			resJson.put("districtID", districtID);
			LendingAgencyCached.deleteCached();
			CityDistrictCached.deleteCached();
		}
		
		resJson.put("resCode", 0);
		sendMsg.setMsgBody(resJson.toJSONString());
		return sendMsg;
	}
	
	
	
}
